/**
 * 
 */
package br.com.ctm.guiamedico.rest.impl;

import java.util.List;

import javax.persistence.PersistenceException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import br.com.ctm.guiamedico.rest.api.EntityResource;
import br.com.framework.negocio.api.BaseNegocio;
import br.com.framework.negocio.exception.NegocioException;
import br.com.framework.persistencia.api.BaseDao;
import br.com.framework.persistencia.model.BaseEntity;

/**
 * @author Cleber
 *
 * @param <E>
 * @param <Negocio>
 */
public abstract class EntityResourceImpl<E extends BaseEntity, Negocio extends BaseNegocio<E, ? extends BaseDao<E>>>
		implements EntityResource<E> {

	@Context
	protected UriInfo uriInfo;

	private Negocio negocio;

	/**
	 * @return the negocio
	 */
	protected Negocio getNegocio() {
		return negocio;
	}

	/**
	 * @param negocio the negocio to set
	 */
	protected void setNegocio(Negocio negocio) {
		this.negocio = negocio;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#findByID(java.lang.Long)
	 */
	@Override
	public E findByID(Long id) throws PersistenceException {
		return getNegocio().findByID(id);
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#findAll()
	 */
	@Override
	public List<E> findAll() throws PersistenceException {
		return getNegocio().findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#findAll(int, int)
	 */
	@Override
	public List<E> findAll(int firstResult, int maxResults)
			throws PersistenceException {
		return getNegocio().findAll(firstResult, maxResults);
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#findByExample(br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public List<E> findByExample(E entidade)
			throws PersistenceException {
		return getNegocio().findByExample(entidade);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#getRecordCount()
	 */
	@Override
	public Long getRecordCount() throws PersistenceException {
		return getNegocio().getRecordCount();
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#insert(br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public Response insert(E entidade) throws NegocioException,
			PersistenceException {
		Response response = null;
		try {
			getNegocio().insert(entidade);
			response = Response.created(uriInfo.getAbsolutePath()).build();
		} catch (Exception e) {
			response = Response.serverError().build();
		}
		return response;
	}


	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#update(java.lang.Long, br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public Response update(Long id, E entidade)
			throws NegocioException, PersistenceException {
		Response response = null;
		try {
			E pEntidade = getNegocio().findByID(id);
			if (pEntidade != null && pEntidade.equals(entidade)) {
				getNegocio().update(entidade);
				response = Response.ok().build();
			} else {
				response = Response.noContent().build();
			}
		} catch (Exception e) {
			response = Response.serverError().build();
		}
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#remove(java.lang.Long)
	 */
	@Override
	public Response remove(Long id) throws NegocioException,
			PersistenceException {
		Response response = null;
		try {
			E pEntidade = getNegocio().findByID(id);
			if (pEntidade != null) {
				getNegocio().remove(pEntidade);
				response = Response.ok().build();
			} else {
				response = Response.noContent().build();
			}
		} catch (Exception e) {
			response = Response.serverError().build();
		}
		return response;
	}
	

}
